package view;

import controllers.ChatBoxInterfaceControllers;
import io.netty.channel.Channel;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import model.database.Function;
import model.util.MessageVerticalBox;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @author zjw
 * @date 2020/1/7 8:59
 */
public class ChatBoxInterface {
    private ArrayList<String> ids = new ArrayList<>();
    private Stage primaryStage;
    private Pane chatPane;
    private ListView chatBoxListView;
    private TextArea messageEditingTextArea;
    private Button sendOutButton;
    private String interfaceId;
    private Channel channel;
    private ChatBoxInterfaceControllers chatBoxInterfaceControllers;

    public String getInterfaceId() {
        return interfaceId;
    }

    public Channel getChannel() {
        return channel;
    }

    public void setInterfaceId(String interfaceId) {
        this.interfaceId = interfaceId;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    /**
     *
     * @param ids 单人聊天，传入需要聊天的账号
     * @param primaryStage
     */
    public ChatBoxInterface(ArrayList<String> ids, Stage primaryStage) throws SQLException {
        this.ids.addAll(ids);
        this.primaryStage = primaryStage;
        this.chatPane = new Pane();
        this.chatBoxListView = new ListView();
        this.messageEditingTextArea = new TextArea();

        chatBoxInterfaceControllers = new ChatBoxInterfaceControllers(ids, primaryStage,
                messageEditingTextArea, chatBoxListView);
        sendOutButton = chatBoxInterfaceControllers.getSendOutButton();
    }

    /**
     * 设置和开启窗口，设置css
     * @return
     * @throws Exception
     */
    public ChatBoxInterface start() throws Exception {
        chatBoxInterfaceControllers.setChannel(channel);
        chatBoxInterfaceControllers.setInterfaceId(interfaceId);
        initInterface();
        Scene scene = new Scene(chatPane, 790, 740);
        scene.getStylesheets().add(ChatInterface.class.getResource("css/ChatInterfaceCss.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.setTitle(ids.get(0));
        primaryStage.show();
        primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("../image/logo.png")));
        return null;
    }

    /**
     * 初始化界面上各组件信息
     */
    private void initInterface() {
        chatBoxListView.setPrefWidth(800);
        chatBoxListView.setPrefHeight(600);
        chatBoxListView.setLayoutX(0);
        chatBoxListView.setLayoutY(0);

        messageEditingTextArea.setPrefWidth(800);
        messageEditingTextArea.setPrefHeight(150);
        messageEditingTextArea.setLayoutX(0);
        messageEditingTextArea.setLayoutY(600);
        messageEditingTextArea.setWrapText(true);

        sendOutButton.setLayoutX(730);
        sendOutButton.setLayoutY(700);

        chatPane.setPrefHeight(750);
        chatPane.setPrefWidth(800);
        chatPane.getChildren().addAll(messageEditingTextArea, chatBoxListView, sendOutButton);
    }
    /**
     * 将接受到的信息直接传到此时的chatBoxListView上，即聊天框上
     * @param id 接收的信息的好友id
     * @param message 接受到的信息
     * @param jud 判断接受到信息时自己的还是好友的
     */
    public void sendMessageToChatBoxListView(String id, String message, int jud) {
        VBox messageVerticalBox = MessageVerticalBox.getMessageVerticalBox(id, message, jud);
        chatBoxListView.getItems().add(messageVerticalBox);
    }

    /**
     * 获得我发给好友的聊天记录和好友发给我的聊天记录，并将其放到聊天界面上
     * @throws SQLException
     */
    public void getChatRecords() throws SQLException {
        // 将数据库中的自己发给好友的信息和好友发给自己的信息取出来
        Function function = new Function();
        Integer fromMeToFriendCount =  function.getPrivateChatRecord(ids.get(1), ids.get(0)).size();
        Integer fromFriendToMeCount =  function.getPrivateChatRecord(ids.get(0), ids.get(1)).size();
        ChatRecord[] fromMeToFriendRecords = function.getPrivateChatRecord(ids.get(1), ids.get(0)).toArray(new ChatRecord[fromMeToFriendCount]);
        ChatRecord[] fromFriendToMeRecords = function.getPrivateChatRecord(ids.get(0), ids.get(0)).toArray(new ChatRecord[fromFriendToMeCount]);
        int i = 0, j = 0;
        while(fromFriendToMeRecords[i] != null && fromMeToFriendRecords[j] != null) {
            if(fromMeToFriendRecords[i].getTime().compareTo(fromFriendToMeRecords[j].getTime()) <= 0) {
                sendMessageToChatBoxListView(fromMeToFriendRecords[i].getId(), fromMeToFriendRecords[i].getMessage(), 1);
                i++;
            } else {
                sendMessageToChatBoxListView(fromFriendToMeRecords[j].getId(), fromFriendToMeRecords[j].getMessage(), 0);
                j++;
            }
        }
        while(fromMeToFriendRecords[i] != null) {
            sendMessageToChatBoxListView(fromMeToFriendRecords[i].getId(), fromMeToFriendRecords[i].getMessage(), 1);
            i++;
        }
        while(fromFriendToMeRecords[j] != null) {
            sendMessageToChatBoxListView(fromFriendToMeRecords[j].getId(), fromFriendToMeRecords[j].getMessage(), 0);
            j++;
        }
    }
}
